# -*- coding: utf-8 -*-
import requests  # 导入requests
import random  # 取随机数
import time
import re
from lxml import etree
from db.mysql_conn import MysqlUtils, config, sqlStr
mysql = MysqlUtils(config)
from config.conf import proxiesDEF, Referer_list
from config.useragent import user_agent_list
get_goods_num = 0
goodsArr = []
user_agent_len = len(user_agent_list) - 10
Referer_list_len = len(Referer_list) - 1


def getAsin():
    try:
        # goodsArr = mysql.query('select id, dp from book where dp = "1119100461" limit 1')
        # goodsArr = mysql.query('select id, dp from book where price = 0 and id < 70000 limit 50')
        goodsArr = mysql.query('select id, dp from book where moneytype="f" limit 50')
    except:
        goodsArr = []
    proxies_list = ['125.46.0.62:53281', '124.11.192.216:80', '124.11.210.123:80', '163.125.223.87:8118',
                    '163.125.223.10:8118', '60.2.44.182:52143', '222.223.182.66:8000', '175.42.68.234:9999',
                    '114.101.45.138:65309', '114.25.152.52:80', '113.108.242.36:47713']
    boo = [True, False, True, False, False, False, False, False]
    try:
        if random.choice(boo):
            proxies_list = proxiesDEF()
            print(proxies_list)
    except:
        print('获取ip失败')
    if len(goodsArr) > 0:
        getGoodsDeDetailTwo(goodsArr, proxies_list)
    else:
        print('=========over==============')


def getGoodsDeDetailTwo (goodsArr, proxies_list):
    global get_goods_num
    for index, goods in enumerate(goodsArr):
        print('==%s==================%s=================================' % (goods['id'], get_goods_num))
        get_goods_num = get_goods_num + 1
        if get_goods_num % 300 == 0:
            print('休息一会==========马上开始')
            time.sleep(200 + random.uniform(13, 66))
        dp = goods['dp']
        url = 'https://www.amazon.com/dp/%s' % dp
        # url = 'https://www.amazon.com/dp/0761513116'
        print(url)
        proxies = {'http': proxies_list[random.randint(0, len(proxies_list)-1)]}
        headers = {
            'User-Agent': user_agent_list[random.randint(0, user_agent_len)],
            'Referer': Referer_list[random.randint(0, Referer_list_len)]
        }
        try:
            session = requests.session()
            response = session.get(url, headers=headers, proxies=proxies, timeout=(3, 7))
            response_soup = etree.HTML(response.content.decode('utf-8'))
            time.sleep(random.uniform(0.5, 3.3))
            # htmlstr = response.content.decode('utf-8')
            try:
                name = response_soup.xpath('//*[@id="productTitle"]/text()')[0].strip()
            except:
                name = ''
            try:
                resArr = response_soup.xpath('//li[@id="SalesRank"]/text()')
                str = ','.join(resArr)
                restr = re.compile('#(.*) \(,\)')
                rankarr = restr.findall(str)[0].split(' in ')
                ranking = rankarr[0].replace(',', '')
                rankingtype = rankarr[1]
            except:
                ranking = -1
                rankingtype = ''
            try:
                ISBN10 = response_soup.xpath('//div/span[contains(text(), "ISBN-10:")]/following-sibling::span[1]/text()')[0].strip()
            except:
                ISBN10 = ''
            try:
                ISBN13 = response_soup.xpath('//div/span[contains(text(), "ISBN-13:")]/following-sibling::span[1]/text()')[0].strip()
            except:
                ISBN13 = ''
            try:
                asin = response_soup.xpath('//input[@id="ASIN"]/@value')[0].strip()
            except:
                asin = ''
            try:
                Paperback = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Paperback")]/../following-sibling::div[1]/span/text()')[0].strip()
                PaperbackDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Paperback")]/../../../../a/@href')[0].strip()
                PaperbackDP = ruturnDP(dp, PaperbackDP)
                print('PaperbackDP  %s' % PaperbackDP)
            except:
                Paperback = ''
                PaperbackDP = ''
            try:
                Hardcover = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Hardcover")]/../following-sibling::div[1]/span/text()')[0].strip()
                HardcoverDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Hardcover")]/../../../../a/@href')[0].strip()
                HardcoverDP = ruturnDP(dp, HardcoverDP)
                print('HardcoverDP  %s' % HardcoverDP)
            except:
                Hardcover = ''
                HardcoverDP = ''
            try:
                Kindle = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Kindle")]/../following-sibling::div[1]/span/text()')[0].strip()
                KindleDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Kindle")]/../../../../a/@href')[0].strip()
                KindleDP = ruturnDP(dp, KindleDP)
                print('KindleDP  %s' % KindleDP)
            except:
                Kindle = ''
                KindleDP = ''
            try:
                Audiobook = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Audiobook")]/../following-sibling::div[1]/span/text()')[0].strip()
                AudiobookDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Audiobook")]/../../../../a/@href')[0].strip()
                AudiobookDP = ruturnDP(dp, AudiobookDP)
                print('AudiobookDP  %s' % AudiobookDP)
            except:
                Audiobook = ''
                AudiobookDP = ''
            try:
                AudioCD = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Audio CD")]/../following-sibling::div[1]/span/text()')[0].strip()
                AudioCDDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Audio CD")]/../../../../a/@href')[0].strip()
                AudioCDDP = ruturnDP(dp, AudioCDDP)
                print('AudioCDDP  %s' % AudioCDDP)
            except:
                AudioCD = ''
                AudioCDDP = ''
            try:
                MP3CD = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "MP3 CD")]/../following-sibling::div[1]/span/text()')[0].strip()
                MP3CDDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "MP3 CD")]/../../../../a/@href')[0].strip()
                MP3CDDP = ruturnDP(dp, MP3CDDP)
                print('MP3CDDP  %s' % MP3CDDP)
            except:
                MP3CD = ''
                MP3CDDP = ''
            try:
                SpiralBound = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Spiral-bound")]/../following-sibling::div[1]/span/text()')[0].strip()
                SpiralBoundDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Spiral-bound")]/../../../../a/@href')[0].strip()
                SpiralBoundDP = ruturnDP(dp, SpiralBoundDP)
                print('SpiralBoundDP  %s' % SpiralBoundDP)
            except:
                SpiralBound = ''
                SpiralBoundDP = ''
            try:
                eTextbook = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "eTextbook")]/../following-sibling::div[1]/span/text()')[0].strip()
                eTextbookDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "eTextbook")]/../../../../a/@href')[0].strip()
                eTextbookDP = ruturnDP(dp, eTextbookDP)
                print('eTextbookDP  %s' % eTextbookDP)
            except:
                eTextbook = ''
                eTextbookDP = ''
            try:
                LooseLeaf = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Loose Leaf")]/../following-sibling::div[1]/span/text()')[0].strip()
                LooseLeafDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Loose Leaf")]/../../../../a/@href')[0].strip()
                LooseLeafDP = ruturnDP(dp, LooseLeafDP)
                print('LooseLeafDP  %s' % LooseLeafDP)
            except:
                LooseLeaf = ''
                LooseLeafDP = ''
            try:
                PlasticComb = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Plastic Comb")]/../following-sibling::div[1]/span/text()')[0].strip()
                PlasticCombDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Plastic Comb")]/../../../../a/@href')[0].strip()
                PlasticCombDP = ruturnDP(dp, PlasticCombDP)
                print('AudiobookDP  %s' % PlasticCombDP)
            except:
                PlasticComb = ''
                PlasticCombDP = ''
            try:
                PrintedAccessCode = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Printed Access Code")]/../following-sibling::div[1]/span/text()')[0].strip()
                PrintedAccessCodeDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Printed Access Code")]/../../../../a/@href')[0].strip()
                PrintedAccessCodeDP = ruturnDP(dp, PrintedAccessCodeDP)
                print('PrintedAccessCodeDP  %s' % PrintedAccessCodeDP)
            except:
                PrintedAccessCode = ''
                PrintedAccessCodeDP = ''
            try:
                UnknownBinding = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Unknown Binding")]/../following-sibling::div[1]/span/text()')[0].strip()
                UnknownBindingDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Unknown Binding")]/../../../../a/@href')[0].strip()
                UnknownBindingDP = ruturnDP(dp, UnknownBindingDP)
                print('UnknownBindingDP  %s' % UnknownBindingDP)
            except:
                UnknownBinding = ''
                UnknownBindingDP = ''
            try:
                BookSupplement = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Book Supplement")]/../following-sibling::div[1]/span/text()')[0].strip()
                BookSupplementDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Book Supplement")]/../../../../a/@href')[0].strip()
                BookSupplementDP = ruturnDP(dp, BookSupplementDP)
                print('BookSupplementDP  %s' % BookSupplementDP)
            except:
                BookSupplement = ''
                BookSupplementDP = ''
            try:
                MultimediaCD = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Multimedia CD")]/../following-sibling::div[1]/span/text()')[0].strip()
                MultimediaCDDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Multimedia CD")]/../../../../a/@href')[0].strip()
                MultimediaCDDP = ruturnDP(dp, MultimediaCDDP)
                print('MultimediaCDDP  %s' % MultimediaCDDP)
            except:
                MultimediaCD = ''
                MultimediaCDDP = ''
            try:
                Ringbound = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Ring-bound")]/../following-sibling::div[1]/span/text()')[0].strip()
                RingboundDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Ring-bound")]/../../../../a/@href')[0].strip()
                RingboundDP = ruturnDP(dp, RingboundDP)
                print('RingboundDP  %s' % RingboundDP)
            except:
                Ringbound = ''
                RingboundDP = ''
            try:
                Pamphlet = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Pamphlet")]/../following-sibling::div[1]/span/text()')[0].strip()
                PamphletDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Pamphlet")]/../../../../a/@href')[0].strip()
                PamphletDP = ruturnDP(dp, PamphletDP)
                print('PamphletDP  %s' % PamphletDP)
            except:
                Pamphlet = ''
                PamphletDP = ''
            try:
                LibraryBinding = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Library Binding")]/../following-sibling::div[1]/span/text()')[0].strip()
                LibraryBindingDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Library Binding")]/../../../../a/@href')[0].strip()
                LibraryBindingDP = ruturnDP(dp, LibraryBindingDP)
                print('LibraryBindingDP  %s' % LibraryBindingDP)
            except:
                LibraryBinding = ''
                LibraryBindingDP = ''
            try:
                DVDROM = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "DVD-ROM")]/../following-sibling::div[1]/span/text()')[0].strip()
                DVDROMDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "DVD-ROM")]/../../../../a/@href')[0].strip()
                DVDROMDP = ruturnDP(dp, DVDROMDP)
                print('DVDROMDP  %s' % DVDROMDP)
            except:
                DVDROM = ''
                DVDROMDP = ''
            try:
                DVD = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "DVD")]/../following-sibling::div[1]/span/text()')[0].strip()
                DVDDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "DVD")]/../../../../a/@href')[0].strip()
                DVDDP = ruturnDP(dp, DVDDP)
                print('DVDDP  %s' % DVDDP)
            except:
                DVD = ''
                DVDDP = ''
            try:
                TextbookBinding = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Textbook Binding")]/../following-sibling::div[1]/span/text()')[0].strip()
                TextbookBindingDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Textbook Binding")]/../../../../a/@href')[0].strip()
                TextbookBindingDP = ruturnDP(dp, TextbookBindingDP)
                print('TextbookBindingDP  %s' % TextbookBindingDP)
            except:
                TextbookBinding = ''
                TextbookBindingDP = ''
            try:
                Cards = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Cards")]/../following-sibling::div[1]/span/text()')[0].strip()
                CardsDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Cards")]/../../../../a/@href')[0].strip()
                CardsDP = ruturnDP(dp, CardsDP)
                print('TextbookBindingDP  %s' % CardsDP)
            except:
                Cards = ''
                CardsDP = ''
            try:
                Map = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Map")]/../following-sibling::div[1]/span/text()')[0].strip()
                MapDP = response_soup.xpath('//div[@class="a-row"]/span[contains(text(), "Map")]/../../../../a/@href')[0].strip()
                MapDP = ruturnDP(dp, MapDP)
                print('TextbookBindingDP  %s' % MapDP)
            except:
                Map = ''
                MapDP = ''

            info = Paperback != '' or Hardcover != '' or Kindle != '' or AudioCD != '' or MP3CD != '' or Audiobook != '' or SpiralBound != '' or eTextbook != '' or LooseLeaf != '' or PlasticComb != '' or PrintedAccessCode != '' or UnknownBinding != '' or BookSupplement != '' or MultimediaCD != '' or Ringbound != '' or Pamphlet != '' or LibraryBinding != '' or DVDROM != '' or DVD != '' or TextbookBinding != '' or Cards != '' or Map != ''
            if info:
                print('=============ssss===========sssss=====================ssssss===================')
            else:
                print('empty =========empty===========empty============empty===========================')
                try:
                    Paperback = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Paperback")]/following-sibling::span[1]/span/text()')[0].strip()
                    PaperbackDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Paperback")]/../../a/@href')[0].strip()
                    PaperbackDP = ruturnDP(dp, PaperbackDP)
                    print('PaperbackDP====  %s' % PaperbackDP)
                except:
                    Paperback = ''
                    PaperbackDP = ''
                try:
                    Hardcover = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Hardcover")]/following-sibling::span[1]/span/text()')[0].strip()
                    HardcoverDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Hardcover")]/../../a/@href')[0].strip()
                    HardcoverDP = ruturnDP(dp, HardcoverDP)
                    print('HardcoverDP  %s' % HardcoverDP)
                except:
                    Hardcover = ''
                    HardcoverDP = ''
                try:
                    Kindle = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Kindle")]/following-sibling::span[1]/span/text()')[0].strip()
                    KindleDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Kindle")]/../../a/@href')[0].strip()
                    KindleDP = ruturnDP(dp, KindleDP)
                    print('KindleDP  %s' % KindleDP)
                except:
                    Kindle = ''
                    KindleDP = ''
                try:
                    Audiobook = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audiobook")]/following-sibling::span[1]/span/text()')[0].strip()
                    AudiobookDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audiobook")]/../../a/@href')[0].strip()
                    AudiobookDP = ruturnDP(dp, AudiobookDP)
                    print('AudiobookDP  %s' % AudiobookDP)
                except:
                    Audiobook = ''
                    AudiobookDP = ''
                try:
                    AudioCD = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audio CD")]/following-sibling::span[1]/span/text()')[0].strip()
                    AudioCDDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audio CD")]/../../a/@href')[0].strip()
                    AudioCDDP = ruturnDP(dp, AudioCDDP)
                    print('AudioCDDP  %s' % AudioCDDP)
                except:
                    AudioCD = ''
                    AudioCDDP = ''
                try:
                    MP3CD = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "MP3 CD")]/following-sibling::span[1]/span/text()')[0].strip()
                    MP3CDDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "MP3 CD")]/../../a/@href')[0].strip()
                    MP3CDDP = ruturnDP(dp, MP3CDDP)
                    print('MP3CDDP  %s' % MP3CDDP)
                except:
                    MP3CD = ''
                    MP3CDDP = ''
                try:
                    SpiralBound = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Spiral-bound")]/following-sibling::span[1]/span/text()')[0].strip()
                    SpiralBoundDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Spiral-bound")]/../../a/@href')[0].strip()
                    SpiralBoundDP = ruturnDP(dp, SpiralBoundDP)
                    print('SpiralBoundDP  %s' % SpiralBoundDP)
                except:
                    SpiralBound = ''
                    SpiralBoundDP = ''
                try:
                    eTextbook = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "eTextbook")]/following-sibling::span[1]/span/text()')[0].strip()
                    eTextbookDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "eTextbook")]/../../a/@href')[0].strip()
                    eTextbookDP = ruturnDP(dp, eTextbookDP)
                    print('eTextbookDP  %s' % eTextbookDP)
                except:
                    eTextbook = ''
                    eTextbookDP = ''
                try:
                    LooseLeaf = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Loose Leaf")]/following-sibling::span[1]/span/text()')[0].strip()
                    LooseLeafDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Loose Leaf")]/../../a/@href')[0].strip()
                    LooseLeafDP = ruturnDP(dp, LooseLeafDP)
                    print('LooseLeafDP  %s' % LooseLeafDP)
                except:
                    LooseLeaf = ''
                    LooseLeafDP = ''
                try:
                    PlasticComb = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Plastic Comb")]/following-sibling::span[1]/span/text()')[0].strip()
                    PlasticCombDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Plastic Comb")]/../../a/@href')[0].strip()
                    PlasticCombDP = ruturnDP(dp, PlasticCombDP)
                    print('PlasticCombDP  %s' % PlasticCombDP)
                except:
                    PlasticComb = ''
                    PlasticCombDP = ''
                try:
                    PrintedAccessCode = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Printed Access Code")]/following-sibling::span[1]/span/text()')[0].strip()
                    PrintedAccessCodeDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Printed Access Code")]/../../a/@href')[0].strip()
                    PrintedAccessCodeDP = ruturnDP(dp, PrintedAccessCodeDP)
                    print('PrintedAccessCodeDP  %s' % PrintedAccessCodeDP)
                except:
                    PrintedAccessCode = ''
                    PrintedAccessCodeDP = ''
                try:
                    UnknownBinding = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Unknown Binding")]/following-sibling::span[1]/span/text()')[0].strip()
                    UnknownBindingDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Unknown Binding")]/../../a/@href')[0].strip()
                    UnknownBindingDP = ruturnDP(dp, UnknownBindingDP)
                    print('UnknownBindingDP  %s' % UnknownBindingDP)
                except:
                    UnknownBinding = ''
                    UnknownBindingDP = ''
                try:
                    BookSupplement = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Book Supplement")]/following-sibling::span[1]/span/text()')[0].strip()
                    BookSupplementDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Book Supplement")]/../../a/@href')[0].strip()
                    BookSupplementDP = ruturnDP(dp, BookSupplementDP)
                    print('BookSupplementDP  %s' % BookSupplementDP)
                except:
                    BookSupplement = ''
                    BookSupplementDP = ''
                try:
                    MultimediaCD = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Multimedia CD")]/following-sibling::span[1]/span/text()')[0].strip()
                    MultimediaCDDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Multimedia CD")]/../../a/@href')[0].strip()
                    MultimediaCDDP = ruturnDP(dp, MultimediaCDDP)
                    print('MultimediaCDDP  %s' % MultimediaCDDP)
                except:
                    MultimediaCD = ''
                    MultimediaCDDP = ''
                try:
                    Ringbound = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Ring-bound")]/following-sibling::span[1]/span/text()')[0].strip()
                    RingboundDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Ring-bound")]/../../a/@href')[0].strip()
                    RingboundDP = ruturnDP(dp, RingboundDP)
                    print('RingboundDP  %s' % RingboundDP)
                except:
                    Ringbound = ''
                    RingboundDP = ''
                try:
                    Pamphlet = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Pamphlet")]/following-sibling::span[1]/span/text()')[0].strip()
                    PamphletDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Pamphlet")]/../../a/@href')[0].strip()
                    PamphletDP = ruturnDP(dp, PamphletDP)
                    print('PamphletDP  %s' % PamphletDP)
                except:
                    Pamphlet = ''
                    PamphletDP = ''
                try:
                    LibraryBinding = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Library Binding")]/following-sibling::span[1]/span/text()')[0].strip()
                    LibraryBindingDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Library Binding")]/../../a/@href')[0].strip()
                    LibraryBindingDP = ruturnDP(dp, LibraryBindingDP)
                    print('LibraryBindingDP  %s' % LibraryBindingDP)
                except:
                    LibraryBinding = ''
                    LibraryBindingDP = ''
                try:
                    DVDROM = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD-ROM")]/following-sibling::span[1]/span/text()')[0].strip()
                    DVDROMDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD-ROM")]/../../a/@href')[0].strip()
                    DVDROMDP = ruturnDP(dp, DVDROMDP)
                    print('DVDROMDP  %s' % DVDROMDP)
                except:
                    DVDROM = ''
                    DVDROMDP = ''
                try:
                    DVD = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD")]/following-sibling::span[1]/span/text()')[0].strip()
                    DVDDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD")]/../../a/@href')[0].strip()
                    DVDDP = ruturnDP(dp, DVDDP)
                    print('DVDDP  %s' % DVDDP)
                except:
                    DVD = ''
                    DVDDP = ''
                try:
                    TextbookBinding = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Textbook Binding")]/following-sibling::span[1]/span/text()')[0].strip()
                    TextbookBindingDP = response_soup.xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Textbook Binding")]/../../a/@href')[0].strip()
                    TextbookBindingDP = ruturnDP(dp, TextbookBindingDP)
                    print('TextbookBindingDP  %s' % TextbookBindingDP)
                except:
                    TextbookBinding = ''
                    TextbookBindingDP = ''



            goods['ranking'] = ranking
            goods['rankingtype'] = rankingtype
            goods['asin'] = asin
            print(name)

            print('Paperback ====   %s   ==' % Paperback)
            print('Hardcover ====   %s   ==' % Hardcover)
            print('Kindle =======   %s   ==' % Kindle)
            print('AudioCD ======   %s   ==' % AudioCD)
            print('MP3CD ======     %s   ==' % MP3CD)
            print('Audiobook ====   %s   ==' % Audiobook)
            print('SpiralBound ===  %s   ==' % SpiralBound)
            print('eTextbook ====   %s   ==' % eTextbook)
            print('Loose Leaf ====  %s   ==' % LooseLeaf)
            print('PlasticComb ==== %s   ==' % PlasticComb)
            print('PrintedAccessCode ====   %s   ==' % PrintedAccessCode)
            print('UnknownBinding =  %s   ==' % UnknownBinding)
            print('BookSupplement =  %s   ==' % BookSupplement)
            print('MultimediaCD === %s   ==' % MultimediaCD)
            print('Ringbound ====   %s   ==' % Ringbound)
            print('Pamphlet ====   %s   ==' % Pamphlet)
            print('LibraryBinding ====   %s   ==' % LibraryBinding)
            print('DVDROM ====     %s   ==' % DVDROM)
            print('DVD ====        %s   ==' % DVD)
            print('TextbookBinding ====        %s   ==' % TextbookBinding)
            print('Cards ====        %s   ==' % Cards)
            print('Map ====        %s   ==' % Map)

            sqlStr = "INSERT INTO newbook (dp,packagetype,packagelink,ISBN10,ISBN13,asin,rankingtype,ranking,star,reviews,moneytype,price,url,name,img,country) values (%s, %s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s)"
            if Paperback != '':
                try:
                    goods['packagetype'] = 'Paperback'
                    Paperback = Paperback.replace('from', '').strip()
                    moneytype = Paperback[0:1]
                    goods['ISBN10'] = PaperbackDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Paperback.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert paperback')
                except Exception as exc:
                    print(exc)
                    print('Paperback=== error')
            if Hardcover != '':
                try:
                    goods['packagetype'] = 'Hardcover'
                    Hardcover = Hardcover.replace('from', '').strip()
                    moneytype = Hardcover[0:1]
                    goods['ISBN10'] = HardcoverDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Hardcover.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Hardcover')
                except:
                    print('Hardcover=== error')
            if Kindle != '':
                try:
                    goods['packagetype'] = 'Kindle'
                    Kindle = Kindle.replace('from', '').strip()
                    moneytype = Kindle[0:1]
                    goods['ISBN10'] = KindleDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Kindle.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Kindle')
                except:
                    print('Kindle=== error')

            if AudioCD != '':
                try:
                    goods['packagetype'] = 'AudioCD'
                    AudioCD = AudioCD.replace('from', '').strip()
                    moneytype = AudioCD[0:1]
                    goods['ISBN10'] = AudioCDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(AudioCD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert AudioCD')
                except:
                    print('AudioCD=== error')
            if MP3CD != '':
                try:
                    goods['packagetype'] = 'MP3CD'
                    MP3CD = MP3CD.replace('from', '').strip()
                    moneytype = MP3CD[0:1]
                    goods['ISBN10'] = MP3CDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(MP3CD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert MP3CD')
                except:
                    print('MP3CD=== error')
            if Audiobook != '':
                try:
                    goods['packagetype'] = 'Audiobook'
                    Audiobook = Audiobook.replace('from', '').strip()
                    moneytype = Audiobook[0:1]
                    goods['ISBN10'] = AudiobookDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Audiobook.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Audiobook')
                except:
                    print('Audiobook=== error')

            if SpiralBound != '':
                try:
                    goods['packagetype'] = 'SpiralBound'
                    SpiralBound = SpiralBound.replace('from', '').strip()
                    moneytype = SpiralBound[0:1]
                    goods['ISBN10'] = SpiralBoundDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(SpiralBound.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert SpiralBound')
                except:
                    print('SpiralBound=== error')
            if eTextbook != '':
                try:
                    goods['packagetype'] = 'eTextbook'
                    eTextbook = eTextbook.replace('from', '').strip()
                    moneytype = eTextbook[0:1]
                    goods['ISBN10'] = eTextbookDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(eTextbook.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert eTextbook')
                except:
                    print('eTextbook=== error')
            if LooseLeaf != '':
                try:
                    goods['packagetype'] = 'LooseLeaf'
                    LooseLeaf = LooseLeaf.replace('from', '').strip()
                    moneytype = LooseLeaf[0:1]
                    goods['ISBN10'] = LooseLeafDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(LooseLeaf.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert LooseLeaf')
                except:
                    print('LooseLeaf=== error')
            if PlasticComb != '':
                try:
                    goods['packagetype'] = 'PlasticComb'
                    PlasticComb = PlasticComb.replace('from', '').strip()
                    moneytype = PlasticComb[0:1]
                    goods['ISBN10'] = PlasticCombDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(PlasticComb.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert PlasticComb')
                except:
                    print('PlasticComb=== error')
            if PrintedAccessCode != '':
                try:
                    goods['packagetype'] = 'PrintedAccessCode'
                    PrintedAccessCode = PrintedAccessCode.replace('from', '').strip()
                    moneytype = PrintedAccessCode[0:1]
                    goods['ISBN10'] = PrintedAccessCodeDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(PrintedAccessCode.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert PrintedAccessCode')
                except:
                    print('PrintedAccessCode=== error')
            if UnknownBinding != '':
                try:
                    goods['packagetype'] = 'UnknownBinding'
                    UnknownBinding = UnknownBinding.replace('from', '').strip()
                    moneytype = UnknownBinding[0:1]
                    goods['ISBN10'] = UnknownBindingDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(UnknownBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert UnknownBinding')
                except:
                    print('UnknownBinding=== error')
            if BookSupplement != '':
                try:
                    goods['packagetype'] = 'BookSupplement'
                    BookSupplement = BookSupplement.replace('from', '').strip()
                    moneytype = BookSupplement[0:1]
                    goods['ISBN10'] = BookSupplementDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(BookSupplement.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert BookSupplement')
                except:
                    print('BookSupplement=== error')
            if MultimediaCD != '':
                try:
                    goods['packagetype'] = 'MultimediaCD'
                    MultimediaCD = MultimediaCD.replace('from', '').strip()
                    moneytype = MultimediaCD[0:1]
                    goods['ISBN10'] = MultimediaCDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(MultimediaCD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert MultimediaCD')
                except:
                    print('MultimediaCD=== error')
            if Ringbound != '':
                try:
                    goods['packagetype'] = 'Ringbound'
                    Ringbound = Ringbound.replace('from', '').strip()
                    moneytype = Ringbound[0:1]
                    goods['ISBN10'] = RingboundDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Ringbound.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Ringbound')
                except:
                    print('Ringbound=== error')
            if Pamphlet != '':
                try:
                    goods['packagetype'] = 'Pamphlet'
                    Pamphlet = Pamphlet.replace('from', '').strip()
                    moneytype = Pamphlet[0:1]
                    goods['ISBN10'] = PamphletDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Pamphlet.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Pamphlet')
                except:
                    print('Pamphlet=== error')
            if LibraryBinding != '':
                try:
                    goods['packagetype'] = 'LibraryBinding'
                    LibraryBinding = LibraryBinding.replace('from', '').strip()
                    moneytype = LibraryBinding[0:1]
                    goods['ISBN10'] = LibraryBindingDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(LibraryBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert LibraryBinding')
                except:
                    print('LibraryBinding=== error')
            if DVDROM != '':
                try:
                    goods['packagetype'] = 'DVDROM'
                    DVDROM = DVDROM.replace('from', '').strip()
                    moneytype = DVDROM[0:1]
                    goods['ISBN10'] = DVDROMDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(DVDROM.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert DVDROM')
                except:
                    print('DVDROM=== error')
            if DVD != '':
                try:
                    goods['packagetype'] = 'DVD'
                    DVD = DVD.replace('from', '').strip()
                    moneytype = DVD[0:1]
                    goods['ISBN10'] = DVDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(DVD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert DVD')
                except:
                    print('DVD=== error')
            if TextbookBinding != '':
                try:
                    goods['packagetype'] = 'TextbookBinding'
                    TextbookBinding = TextbookBinding.replace('from', '').strip()
                    moneytype = TextbookBinding[0:1]
                    goods['ISBN10'] = TextbookBindingDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(TextbookBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert TextbookBinding')
                except:
                    print('TextbookBinding=== error')
            if Cards != '':
                try:
                    goods['packagetype'] = 'Cards'
                    Cards = Cards.replace('from', '').strip()
                    moneytype = Cards[0:1]
                    goods['ISBN10'] = CardsDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Cards.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Cards')
                except:
                    print('Cards=== error')
            # if Map != '':
            #     try:
            #         goods['packagetype'] = 'Map'
            #         Map = Map.replace('from', '').strip()
            #         moneytype = Map[0:1]
            #         goods['ISBN10'] = MapDP
            #         goods['moneytype'] = moneytype
            #         goods['price'] = averageNum(Map.replace(moneytype, ''))
            #         sqlParam = returnGoods(goods)
            #         mysql.insert(sqlStr, sqlParam)
            #         print('insert Map')
            #     except:
            #         print('Map=== error')

            info1 = Paperback != '' or Hardcover != '' or Kindle != '' or AudioCD != '' or MP3CD != '' or Audiobook != '' or SpiralBound != '' or eTextbook != '' or LooseLeaf != '' or PlasticComb != '' or PrintedAccessCode != '' or UnknownBinding != '' or BookSupplement != '' or MultimediaCD != '' or Ringbound != '' or Pamphlet != '' or LibraryBinding != '' or DVDROM != '' or DVD != '' or TextbookBinding != '' or Cards != '' or Map != ''
            if info1:
                try:
                    print('deletesql')
                    deletesql = "DELETE FROM newbook WHERE id =  %s" % (goods['id'])
                    mysql.delete(deletesql)
                except:
                    print('deletesql   === error')

            goods['ranking'] = ranking
            goods['rankingtype'] = rankingtype
            goods['ISBN10'] = ISBN10
            goods['ISBN13'] = ISBN13
            goods['asin'] = asin
            print(name)
            print('Paperback ==== %s  ' % (Paperback))
            print(Paperback)
            print('Hardcover ==== %s  ' % (Hardcover))
            print(Hardcover)
            if Paperback != '' and Hardcover != '':
                try:
                    sql = 'select id, dp, star, reviews, url, name, img, country from book where id= %s' % (goods['id'])
                    deleteGoods = mysql.query(sql)[0]
                    if Paperback != '':
                        try:
                            goods['packagetype'] = 'Paperback'
                            Paperback = Paperback.replace('from', '').strip()
                            moneytype = Paperback[0:1]
                            goods['moneytype'] = moneytype
                            goods['price'] = averageNum(Paperback.replace(moneytype, ''))
                            sqlParam = returnGoods(goods, deleteGoods)
                            mysql.insert(sqlStr, sqlParam)
                            print('double insert paperback')
                        except:
                            print('Paperback and Hardcover   Paperback=== error')

                    if Hardcover != '':
                        try:
                            goods['packagetype'] = 'Hardcover'
                            Hardcover = Hardcover.replace('from', '').strip()
                            moneytype = Hardcover[0:1]
                            goods['moneytype'] = moneytype
                            goods['price'] = averageNum(Hardcover.replace(moneytype, ''))
                            sqlParam = returnGoods(goods, deleteGoods)
                            mysql.insert(sqlStr, sqlParam)
                            print('double insert Hardcover')
                        except:
                            print('Paperback and Hardcover   Hardcover=== error')

                    deletesql = "DELETE FROM book WHERE id =  %s" % (goods['id'])
                    mysql.delete(deletesql)
                except:
                    print('Paperback and Hardcover   === error')
            else:
                if Paperback != '':
                    try:
                        goods['packagetype'] = 'Paperback'
                        Paperback = Paperback.replace('from', '').strip()
                        moneytype = Paperback[0:1]
                        goods['moneytype'] = moneytype
                        goods['price'] = averageNum(Paperback.replace(moneytype, ''))
                        print(goods)
                        updateSql(goods)
                        print('Paperback   update')
                    except:
                        print('Paperback   err')
                if Hardcover != '':
                    try:
                        goods['packagetype'] = 'Hardcover'
                        Hardcover = Hardcover.replace('from', '').strip()
                        moneytype = Hardcover[0:1]
                        goods['moneytype'] = moneytype
                        goods['price'] = averageNum(Hardcover.replace(moneytype, ''))
                        print(goods)
                        updateSql(goods)
                        print('Hardcover   update')
                    except Exception as exc:
                        print(exc)
                        print('Hardcover   err')
                if Paperback == '' and Hardcover == '' and name !='xxxxxxxxx':
                    try:
                        goods['packagetype'] = 'other'
                        goods['moneytype'] = ''
                        goods['price'] = -1
                        print(goods)
                        updateSql(goods)
                        print('packagetype Hardcover xxxxxxxxx updateSql')
                    except Exception as exc:
                        print(exc)
                        print('packagetype Hardcover xxxxxxxxx  err')

            # 清空数组 goods
            print('*goods***********************************')
            goods.clear()
            print(goods)

        except Exception as exc:
            print(exc)
            print('get 请求失败 跳往下一个')
    getAsin()


def updateSql(goods):
    d = {
        'packagetype': goods['packagetype'],
        'ISBN10': goods['ISBN10'],
        'ISBN13': goods['ISBN13'],
        'asin': goods['asin'],
        'rankingtype': goods['rankingtype'],
        'ranking': goods['ranking'],
        'moneytype': goods['moneytype'],
        'price': goods['price'],
    }  # 更新的字段及对应值
    t = ('id', goods['id'])  # 更新所需条件
    table_name = 'book'
    updateSqlStr = 'UPDATE %s SET ' % table_name + ','.join(['%s=%r' % (k, d[k]) for k in d]) + ',updateTime = NOW() WHERE %s=%r;' % (t[0], t[1])
    mysql.update(updateSqlStr)


def returnGoods(goods, deleteGoods):
    sqlParam = [
        goods['dp'],
        goods['packagetype'],
        '',
        goods['ISBN10'],
        '',
        '',
        goods['rankingtype'],
        goods['ranking'],
        goods['star'],
        goods['reviews'],
        goods['moneytype'],
        goods['price'],
        goods['url'],
        goods['name'],
        goods['img'],
        goods['country']
    ]
    return sqlParam


def averageNum(str):
    try:
        if ' - ' in str:
            arr = str.split(' - ')
            add = float(arr[0]) + float(arr[1])
            price = '%.2f' % float(add/2)
        else:
            price = '%.2f' % float(str)
    except:
        price = -1
    return price

def ruturnDP(_dp, href):
    if href == 'javascript:void(0)':
        return _dp
    dp = href.split('dp/')[1].split('/ref')[0]
    return dp

if __name__ == '__main__':
    getAsin()




